<?php
/**
 * Builds up a programmatic model for stacked SQL ALTER clauses to be used by ALTER query.
 */
abstract class SQLAlterClause extends AbstractSQLClause {
	
	/**
	 * Adds a column to table.
	 * 
	 * @param SQLColumnDefinitionClause $objColumnDefinition
	 * @param string $strColumnNameBefore
	 * @return SQLAlterClause
	 */
	public function addColumn($objColumnDefinition, $strColumnNameBefore="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "COLUMN";
		$tblTMP["OPERATION"] = "ADD";
		$tblTMP["COLUMN"]=$objColumnDefinition;
		if($strColumnNameBefore) $tblTMP["BEFORE_COLUMN"]=$strColumnNameBefore;
		$this->tblContents[]=$tblTMP;
		return this;
	}

	/**
	 * Modifies column info in table.
	 * 
	 * @param SQLColumnDefinitionClause $objNewColumnDefinition
	 * @param string $strColumnNameBefore
	 * @return SQLAlterClause
	 */
	public function changeColumn($objNewColumnDefinition, $strColumnNameBefore="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "COLUMN";
		$tblTMP["OPERATION"] = "CHANGE";
		$tblTMP["COLUMN"] = $objNewColumnDefinition;
		if($strColumnNameBefore) $tblTMP["BEFORE_COLUMN"] = $strColumnNameBefore;
		$this->tblContents[]=$tblTMP;
		return this;
	}
	
	/**
	 * Renames column in table.
	 * 
	 * @param string $strOldColumnName
	 * @param string $strNewColumnName
	 * @return SQLAlterClause
	 */
	public function renameColumn($strOldColumnName, $strNewColumnName) {
		$tblTMP = array();
		$tblTMP["TYPE"] = "COLUMN";
		$tblTMP["OPERATION"] = "RENAME";
		$tblTMP["OLD_COLUMN_NAME"] = $strOldColumnName;
		$tblTMP["NEW_COLUMN_NAME"] = $strNewColumnName;
		$this->tblContents[]=$tblTMP;
		return this;
	}
	
	/**
	 * Drops a column from table
	 * 
	 * @param string $strColumnName
	 * @return SQLAlterClause
	 */
	public function dropColumn($strColumnName) {
		$tblTMP = array();
		$tblTMP["TYPE"] = "COLUMN";
		$tblTMP["OPERATION"] = "DROP";
		$tblTMP["NAME"] =  $strColumnName;
		$this->tblContents[]=$tblTMP;
		
		return this;
	}
	
	/**
	 * Adds primary key to table
	 * 
	 * @param SQLColumnsClause $objColumns
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function addPrimaryKey($objColumns, $strIndexName="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "PRIMARY_KEY";
		$tblTMP["OPERATION"] = "ADD";
		$tblTMP["COLUMNS"] = $objColumns;
		if($strIndexName) $tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;
		
		return this;
	}
	
	/**
	 * Drops a primary key
	 * 
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function dropPrimaryKey($strIndexName="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "PRIMARY_KEY";
		$tblTMP["OPERATION"] = "DROP";
		if($strIndexName) $tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;
		
		return this;
	}
	
	/**
	 * Adds an unique index 
	 * 
	 * @param SQLColumnsClause $objColumns
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function addUnique($objColumns, $strIndexName="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "UNIQUE_KEY";
		$tblTMP["OPERATION"] = "ADD";
		$tblTMP["COLUMNS"] = $objColumns;
		if($strIndexName) $tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;
		
		return this;
	}
	
	/**
	 * Drops an index.
	 * 
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function dropUnique($strIndexName) {
		$tblTMP = array();
		$tblTMP["TYPE"] = "UNIQUE_KEY";
		$tblTMP["OPERATION"] = "DROP";
		$tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;		
		return this;
	}
	
	/**
	 * Adds a foreign key to table
	 * 
	 * @param string $strColumnName
	 * @param string $strReferencedTableName
	 * @param string $strReferencedColumnName
	 * @param string $strOnDeleteAction
	 * @param string $strOnUpdateAction
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function addForeignKey($strColumnName, $strReferencedTableName, $strReferencedColumnName, $strOnDeleteAction="", $strOnUpdateAction="", $strIndexName="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "FOREIGN_KEY";
		$tblTMP["OPERATION"] = "ADD";
		$tblTMP["COLUMN"] = $strColumnName;
		$tblTMP["LINKED_TABLE"] = $strReferencedTableName;
		$tblTMP["LINKED_COLUMN"] = $strReferencedColumnName;
		if($strOnDeleteAction) $tblTMP["DELETE_ACTION"] = $strOnDeleteAction;
		if($strOnUpdateAction) $tblTMP["UPDATE_ACTION"] = $strOnUpdateAction;
		if($strIndexName) $tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;		
		return this;
	}
	
	/**
	 * Drops a foreign key
	 * 
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function dropForeignKey($strIndexName) {
		$tblTMP = array();
		$tblTMP["TYPE"] = "FOREIGN_KEY";
		$tblTMP["OPERATION"] = "DROP";
		$tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;
		return this;
	}
	
	/**
	 * Adds an index
	 * 
	 * @param SQLOptionsClause $objColumns
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function addIndex($objColumns, $strIndexName="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "KEY";
		$tblTMP["OPERATION"] = "ADD";
		$tblTMP["COLUMNS"] = $objColumns;
		if($strIndexName) $tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;		
		return this;
	}
	
	/**
	 * Drops an index
	 * 
	 * @param string $strIndexName
	 * @return SQLAlterClause
	 */
	public function dropIndex($strIndexName) {
		$tblTMP = array();
		$tblTMP["TYPE"] = "KEY";
		$tblTMP["OPERATION"] = "DROP";
		$tblTMP["NAME"] = $strIndexName;
		$this->tblContents[]=$tblTMP;
		return this;
	}
	
	/**
	 * Modifies a table's autoincrement value.
	 * 
	 * @param integer $intNewValue
	 * @return SQLAlterClause
	 */
	public function setAutoIncrementValue($intNewValue) {
		$tblTMP = array();
		$tblTMP["TYPE"] = "AUTO_INCREMENT";
		$tblTMP["VALUE"] = $intNewValue;
		$this->tblContents[]=$tblTMP;
		return this;
	}
	
	/**
	 * Modifies a table's type
	 * 
	 * @param string $strTableType
	 * @return SQLAlterClause
	 */
	public function setTableType($strTableType) {
		$tblTMP = array();
		$tblTMP["TYPE"] = "ENGINE";
		$tblTMP["VALUE"] = $strTableType;
		$this->tblContents[]=$tblTMP;
		return this;
	}
}